//
//  TSWalletViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/8.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSWalletViewController: TSBaseViewController {
    
    let tableHeaderViewH : CGFloat = 194
    var tableController : TSBaseTableViewController!
    let dataSource : [TSNormalCellModel] = [TSNormalCellModel.init(title: "充值记录", value: "在这里查看您近期的充值情况", textColor: "", font: UIFont()), TSNormalCellModel.init(title: "提现记录", value: "在这里查看您近期的提现情况", textColor: "", font: UIFont())]
    lazy var headerView : TSWalletHeaderView = {
        let header = TSWalletHeaderView.init(frame: CGRect(x: 0, y: -tableHeaderViewH, width: SCREEN_WIDTH, height: tableHeaderViewH))
        return header
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configViews()
        setupNavigationTitle(title: "我的钱包")
    }
    
}

extension TSWalletViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = dataSource[indexPath.row]
        let vc = TSAccountRecordViewController()
        switch model.title {
        case "充值记录":
            vc.recordType = TSRecordType.rechargeType
        case "提现记录":
            vc.recordType = TSRecordType.withdrawType
        default:
            vc.recordType = TSRecordType.unknownType
        }
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        62
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.001))
        header.backgroundColor = WHITE_COLOR
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        0.001
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let footer = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.001))
        footer.backgroundColor = WHITE_COLOR
        return footer
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0.001
    }
}

extension TSWalletViewController : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSWalletTableViewCell.self)) as? TSWalletTableViewCell
        
        if cell == nil {
            cell = TSWalletTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSWalletTableViewCell.self))
        }
        
        let last = indexPath.row == dataSource.count - 1
        cell?.setupCell(model: dataSource[indexPath.row], last: last)
        
        return cell!
    }
}

// MARK: - UIScrollViewDelegate
extension TSWalletViewController : UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let offsetY = scrollView.contentOffset.y + scrollView.contentInset.top

        if offsetY < 0 {

            var rect = headerView.frame
            rect.origin.y = offsetY - tableHeaderViewH
            rect.size.height = tableHeaderViewH - offsetY
            headerView.frame = rect
            headerView.updateHeaderViewFrame(offsetY: abs(offsetY))
        }
    }
}

// MARK: - Initialize
extension TSWalletViewController {
    
    func configViews() {
            
        tableController = TSBaseTableViewController(style: .grouped)
        tableController.tableView.contentInset = UIEdgeInsets(top: tableHeaderViewH, left: 0, bottom: 0, right: 0)
        tableController.tableView.backgroundColor = WHITE_COLOR
        tableController.tableView.delegate = self;
        tableController.tableView.dataSource = self
        tableController.tableView.separatorStyle = .none
        self.view.addSubview(tableController.tableView)
        tableController.tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }
    
        tableController.tableView.addSubview(headerView)
    }
}
